#!/bin/bash

set -e

REDIS_NAME="buildfarm-redis"
REDIS_IMAGE="redis:7.2.4"

SERVER_NAME="buildfarm-server"
SERVER_IMAGE="bazelbuild/buildfarm-server:latest"

WORKER_NAME="buildfarm-worker"
WORKER_IMAGE="bazelbuild/buildfarm-worker:latest"

CONFIG="config.minimal.yml"

# Start Buildfarm Cluster
start_buildfarm () {
  # Run Redis Container
  docker run -d --name $REDIS_NAME -p 6379:6379 $REDIS_IMAGE

  # Run Buildfarm Server Container
  docker run -d --name $SERVER_NAME -v $(pwd)/examples:/var/lib/buildfarm-server -p 8980:8980 --network host \
  $SERVER_IMAGE /var/lib/buildfarm-server/$CONFIG -p 8980

  # Run Buildfarm Shard Worker Container
  mkdir -p /tmp/worker
  docker run -d --name $WORKER_NAME --privileged -v $(pwd)/examples:/var/lib/buildfarm-shard-worker \
  -v /tmp/worker:/tmp/worker -p 8981:8981 --network host \
  $WORKER_IMAGE /var/lib/buildfarm-shard-worker/$CONFIG --public_name=localhost:8981

  echo "Buildfarm cluster started with endpoint: localhost:8980"
}

stop_buildfarm () {
  docker stop buildfarm-server && docker rm buildfarm-server
  docker stop buildfarm-worker && docker rm buildfarm-worker
  docker stop buildfarm-redis && docker rm buildfarm-redis
}

restart_buildfarm () {
  stop_buildfarm
  start_buildfarm
}

init () {
  case "$1" in
  start)
    start_buildfarm
    ;;
  stop)
    stop_buildfarm
    ;;
  restart)
    restart_buildfarm
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart}"
    exit 1
esac
}

init $1
